package com.example.usersvc.service;

import com.example.usersvc.domain.User;
import com.example.usersvc.domain.UserDto;
import com.example.usersvc.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public Optional<User> findByName(String name) {
        //[GET] this.userRepository.findAll()
        //[POST] this.userRepository.save()
        //[Delete] this.userRepository.deleteAllById();
        return this.userRepository.findByName(name);
    }

    public void deleteById(String id){
        this.userRepository.deleteById(id);
    }

    public User save(UserDto userDto){
        User user = new User();
        user.setName(userDto.getName());
        user.setRole(userDto.getRole());
        user.setPassword_hash(userDto.getPassword());
        return  this.userRepository.save(user);
    }

    public List<User> findAll() {
        return this.userRepository.findAll();
    }


}
